package qiniuyun;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

public class Upload {
    public static String accessKey = "lX1jbNI6pmtl0mKmfFlJ2Eca3jMvCCEc9ehdcZho";
    public static String secretKey = "NAptHieMatkiA94mYP-fX-KnpvcE49w9mW0b2jKs";
    public static String bucket = "kp-community-test";
    public static String filePath = "C:\\Users\\LZKJ\\Pictures\\Saved Pictures\\kp.png";
    public static String key = null;
    public static void main(String[] args) {
        Configuration cfg = new Configuration();
        cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V1;

        UploadManager uploadManager = new UploadManager(cfg);

        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);

        try{
            Response response = uploadManager.put(filePath, key, upToken);
            //解析上传成功结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);

        }catch (QiniuException ex){
            Response response = ex.response;
            System.err.println(response.toString());
            try {
                System.out.println(response.body());
            }catch (QiniuException ex2){
                System.out.println("===");
            }
        }
    }
}
